#include <glib.h>

static void _queue_foreach_func(gpointer data, gpointer       user_data)
{
    g_print("user_data:%s, data:%d \n", (gchar *)user_data, GPOINTER_TO_INT(data));
}

gint main(gint argc, gchar **argv)
{
    GQueue *q = NULL;
    gint ret1 = 0, ret2 = 0, ret3 = 0;
    GList *l1 = NULL, *l2 = NULL, *l3 = NULL;

    q = g_queue_new();

    g_queue_push_tail(q, GINT_TO_POINTER(1));
    g_queue_push_tail(q, GINT_TO_POINTER(3));
    g_queue_push_tail(q, GINT_TO_POINTER(5));
    g_queue_push_tail(q, GINT_TO_POINTER(7));
    g_queue_push_tail(q, GINT_TO_POINTER(9));
    // result: 1-3-5-7-9

    g_queue_foreach(q, _queue_foreach_func, "Queue-push");

    // peek
    ret1 = GPOINTER_TO_INT(g_queue_peek_head(q));
    ret2 = GPOINTER_TO_INT(g_queue_peek_tail(q));
    ret3 = GPOINTER_TO_INT(g_queue_peek_nth(q, 2));
    l1 = g_queue_peek_head_link(q);
    l2 = g_queue_peek_tail_link(q);
    l3 = g_queue_peek_nth_link(q, 2);

    g_print("ret1:%d, ret2:%d, ret3:%d. l1->data:%d, l2->data:%d, l3->data:%d \n",
        ret1, ret2, ret3, GPOINTER_TO_INT(l1->data), GPOINTER_TO_INT(l2->data), 
        GPOINTER_TO_INT(l3->data));

    g_queue_foreach(q, _queue_foreach_func, "Queue-after-peek");
    
    g_queue_free(q);

    return 0;
}